/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.berlin.ejb.facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import ru.ssau.berlin.entity.Event;
import ru.ssau.berlin.entity.EventAttribute;
import ru.ssau.berlin.entity.EventParameter;
import ru.ssau.berlin.entity.EventType;
import ru.ssau.berlin.entity.Person;
import ru.ssau.berlin.entity.PersonGroup;
import ru.ssau.berlin.entity.PersonRole;

/**
 * 
 * @author Caesar
 */
@Stateless
public class EventFacade implements EventFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(Event event) {
        em.persist(event);
    }

    public void edit(Event event) {
        for (EventParameter parameter: event.getParameters()) {
            em.merge(parameter);
        }
        for (PersonRole role: event.getRoles()) {
            em.merge(role);
        }
        em.merge(event);
    }

    public void remove(Event event) {
        Event temp = em.merge(event);
        for (EventParameter parameter: temp.getParameters()) {
            em.remove(parameter);
        }
        em.remove(temp);
    }

    public Event find(long id) {
        return em.find(Event.class, id);
    }

    public List<Event> findAll() {
        return em.createNamedQuery("findAllEvents").getResultList();
    }

    public List<Event> findInstructorSchedule(Person instructor) {
        return em.createNamedQuery("findInstructorSchedule")
                .setParameter("instructor", instructor).getResultList();
    }

    public List<Event> findGroupSchedule(PersonGroup group) {
        return em.createNamedQuery("findGroupSchedule")
                .setParameter("group", group).getResultList();
    }

    public List<Event> findStudentSchedule(Person student, String day) {
        return em.createNamedQuery("findStudentSchedule")
                .setParameter("student", student).setParameter("day", day).getResultList();
    }

    public List<Event> findByName(String name) {
        return em.createNamedQuery("findEventByName")
                .setParameter("eventName", name).getResultList();
    }

    public EventType findEventType(String name) {
        return (EventType) em.createNamedQuery("findEventTypeByName")
                .setParameter("typeName", name).getSingleResult();
    }

    public EventType findEventType(long id) {
        return em.find(EventType.class, id);
    }

    public List<EventType> findEventTypes() {
        return em.createNamedQuery("findEventTypes").getResultList();
    }


    public void createType(EventType type) {
        em.persist(type);
    }

    public void editType(EventType type) {
        em.merge(type);
    }

    public void removeType(EventType type) {
        em.remove(em.merge(type));
    }

    public void createAttribute(EventAttribute attr) {
        em.persist(attr);
    }

    public void editAttribute(EventAttribute attr) {
        em.merge(attr);
    }

    public void removeAttribute(EventAttribute attr) {
        em.remove(em.merge(attr));
    }

    public List<EventAttribute> findEventAttributes() {
        List<EventAttribute> result = em.createNamedQuery("findEventAttributes").getResultList();
        // for lazy list evaluation in the current session
        // for (EventAttribute attr : result) {
        //    attr.getEventTypes().size();
        //}
        return result;
    }

    public EventAttribute findEventAttribute(long id) {
        return (EventAttribute) em.find(EventAttribute.class, id);
    }

    public EventAttribute findEventAttribute(String name) {
        return (EventAttribute) em.createNamedQuery("findEventAttributeByName")
                .setParameter("attrName", name).getSingleResult();
    }

    public void createParameter(EventParameter parameter) {
        em.persist(parameter);
    }

    public EventParameter findParameter(long id) {
        return em.find(EventParameter.class, id);
    }

    public void removeParameter(EventParameter parameter) {
        parameter = findParameter(parameter.getId());
        em.remove(parameter);
    }

}
